package com.zr.sms.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.sms.dao.GoodsListDao;
import com.zr.sms.model.Goodslist;
import com.zr.sms.model.Shoplist;
import com.zr.sms.util.JDBCUtil;

public class GoodsListDaoImpl implements GoodsListDao{

	@Override
	public boolean addGoodsList(String gdate, String gcontent,double gtotalmoney, int eid) {
	      Connection con=null;
	      PreparedStatement stmt=null;
	      boolean flag=false;
	      con=JDBCUtil.getConnection();
	      StringBuffer sql=new StringBuffer("insert into goodslist (gdate,gcontent,gtotalmoney,eid) values(?,?,?,?)");
	      try {
			stmt=con.prepareStatement(sql.toString());
			stmt.setString(1, gdate);
			stmt.setString(2, gcontent);
			stmt.setDouble(3, gtotalmoney);
			stmt.setInt(4, eid);
			int a=stmt.executeUpdate();
			if(a>0){
				flag=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	      
		return flag;
	}

	@Override
	public Goodslist selectGoodsListBySid(int gid) {
		Connection con=null;
		PreparedStatement stmt=null;
		ResultSet rs=null;
		Goodslist g=new Goodslist();
		
		con=JDBCUtil.getConnection();
		StringBuffer sql=new StringBuffer("select * from goodslist where gid=?");
		
		try {
			stmt=con.prepareStatement(sql.toString());
			stmt.setInt(1, gid);
			rs=stmt.executeQuery();
			if(rs.next()){
				g.setGid(rs.getInt("gid"));
				g.setGdate(rs.getString("gdate"));
				g.setGcontent(rs.getString("gcontent"));
				g.setGtotalmoney(rs.getDouble("gtotalmoney"));
				g.setEid(rs.getInt("eid"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return g;
		
	}




	@Override
	public int getGlsConut() {
		int scount = 0;
		Connection   con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select count(gid) scount from goodslist");
		try {
			PreparedStatement   pst =  con.prepareStatement(sql.toString());
			
			ResultSet  rs = pst.executeQuery();
			if(rs.next()) {
				scount = rs.getInt("scount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return scount;
	}

	@Override
	public List<Goodslist> getGoodsLists(int start, int pageSize) {
		List<Goodslist> ls = new ArrayList<Goodslist>();
		Connection   con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select gid,gdate,gcontent,gtotalmoney,eid from  goodslist");
		sql.append(" limit ?,?");
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, start);
			pst.setInt(2, pageSize);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Goodslist g = new Goodslist();
				g.setGid(rs.getInt("gid"));
				g.setGdate(rs.getString("gdate"));
				g.setGcontent(rs.getString("gcontent"));
				g.setGtotalmoney(rs.getDouble("gtotalmoney"));
				g.setEid(rs.getInt("eid"));
				ls.add(g);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ls;
	}
	@Override
	public int getGlConut(String searchdate) {
		int  scount =  0;
		Connection   con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select count(gid) scount from  goodslist where gdate like ?");
		try {
			PreparedStatement   pst =  con.prepareStatement(sql.toString());
			pst.setString(1, "%"+searchdate+"%");
			ResultSet  rs = pst.executeQuery();
			if(rs.next()) {
				scount = rs.getInt("scount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return scount;
	}


	@Override
	public List<Goodslist> getGoodslists(int start, int pageSize, String searchdate) {
		List<Goodslist>  gls = new ArrayList<Goodslist>();
		Connection   con = JDBCUtil.getConnection();
		StringBuffer sql = new StringBuffer("select gid,gdate,gcontent,gtotalmoney,eid from  goodslist");
		sql.append(" where gdate like ? ");
		sql.append(" limit ?,?");
		try {
			PreparedStatement   pst =  con.prepareStatement(sql.toString());
			pst.setString(1, "%"+searchdate+"%");
			pst.setInt(2, start);
			pst.setInt(3, pageSize);
			ResultSet  rs = pst.executeQuery();
			while(rs.next()) {
				Goodslist g = new Goodslist();
				g.setGid(rs.getInt("gid"));
				g.setGdate(rs.getString("gdate"));
				g.setGcontent(rs.getString("gcontent"));
				g.setGtotalmoney(rs.getDouble("gtotalmoney"));
				g.setEid(rs.getInt("eid"));
				gls.add(g);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return gls;
}
}


